package hk.m4s.carfriends.fragments;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.facebook.drawee.view.SimpleDraweeView;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.exception.ApiException;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextSubListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import hk.m4s.carfriends.AppConf;
import hk.m4s.carfriends.R;
import hk.m4s.carfriends.base.BaseFragment;
import hk.m4s.carfriends.constract.INews;
import hk.m4s.carfriends.model.Datas;
import hk.m4s.carfriends.model.api.SubjectPostApi;
import hk.m4s.carfriends.model.resulte.SubjectResulte;
import hk.m4s.carfriends.presenter.NewsPresenterImpl;
import hk.m4s.carfriends.services.NewsServices;
import rx.Observable;

/**
 * Created by Administrator on 2016/12/2.
 */

public class HomeFragment extends BaseFragment implements View.OnClickListener, HttpOnNextListener, HttpOnNextSubListener
{

    @BindView(R.id.home_recycler)
    RecyclerView mHomeRecycler;
    SubjectPostApi postEntity;
    @Override
    public void onClick(View view) {

    }

    @BindView(R.id.home_refresh)
    SwipeRefreshLayout mHomeRefresh;


    private Unbinder mUnbinder;
    //private BaseRecyclerAdapter<Datas.ResultBean.DataBean> mAdapter;
    private NewsPresenterImpl mPresenter;
//    private List<Banners> mList;

    private List<String> banner_img;
    private List<String> banner_url;

    private boolean connect = false;//判断网络是否连接正常
    private Handler mHandler;

     HttpManager httpManager;

    @Override
    public int getLayoutId()
    {
        checkNet();
        return R.layout.fragment_home;
    }

    @Override
    public void initVariables()
    {

        //mPresenter = new NewsPresenterImpl(getActivity(), this);
//        mList = new ArrayList<>();
        banner_img = new ArrayList<>();
        banner_url = new ArrayList<>();
        banner_img = Arrays.asList(AppConf.BANNER_IMGS);
        banner_url = Arrays.asList(AppConf.BANNER_URL);
        mHandler = new Handler();
    }

    @Override
    public void initViews(View view, Bundle savedInstanceState)
    {
        httpManager=new HttpManager(this,getActivity());
        mUnbinder = ButterKnife.bind(this, view);
        mHomeRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        postEntity = new SubjectPostApi();
        postEntity.setAll(true);
        postEntity.getMothed();
        httpManager.doHttpDeal(postEntity);
//        mAdapter = new BaseRecyclerAdapter<Datas.ResultBean.DataBean>(getActivity(), null, R.layout.item)
//        {
//            @Override
//            protected void convert(BaseViewHolder helper, final Datas.ResultBean.DataBean item)
//            {
//                helper.setText(R.id.author_name, item.getAuthor_name());
//                String title = item.getTitle();
//                if (title.length() > 15)
//                {
//                    String s = title.substring(0, 15) + "...";
//                    helper.setText(R.id.text_title, s);
//                }else
//                {
//                    helper.setText(R.id.text_title, title);
//                }
//                helper.setText(R.id.realtype, item.getRealtype());
//                helper.setText(R.id.date, item.getDate());
//                SimpleDraweeView imageViews = (SimpleDraweeView) helper.getConvertView().findViewById(R.id.thumbnail);
//                imageViews.setImageURI(item.getThumbnail_pic_s());
//                RelativeLayout layout = (RelativeLayout) helper.getConvertView().findViewById(R.id.home_item);
//                layout.setOnClickListener(new View.OnClickListener()
//                {
//                    @Override
//                    public void onClick(View v)
//                    {
//                        Intent it = new Intent(getActivity(), DetailActivity.class);
//                        it.putExtra(AppConf.NEWS, item.getUrl());
//                        getActivity().startActivity(it);
//                    }
//                });
//            }
//        };
//        mHomeRecycler.setAdapter(mAdapter);

        mHomeRefresh.setColorSchemeResources(R.color.colorBG);
        mHomeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
        {
            @Override
            public void onRefresh()
            {
                mHomeRefresh.setRefreshing(true);
                initLoadData();
                mHandler.postDelayed(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        mHomeRefresh.setRefreshing(false);
                    }
                }, 1200);
            }
        });



    }

    public void checkNet()//网络是否已经连接上
    {
        ConnectivityManager manager = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = manager.getActiveNetworkInfo();
        connect = info != null && info.isConnected();
    }




    @Override
    public void initLoadData()
    {
        if (connect)
        {
            httpManager.doHttpDeal(postEntity);
            //mPresenter.loadingDatasFromNet();
        }else
        {
           // mPresenter.loadingDatasFromCache();
        }
    }

    @Override
    public void onDestroyView()
    {
        super.onDestroyView();
        mUnbinder.unbind();
    }



    @Override
    public void onNext(String resulte, String mothead) {

        Toast.makeText(getContext(),resulte.toString(),Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onError(ApiException e) {

    }


    @Override
    public void onNext(Observable observable) {

    }

}
